﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;

namespace Common
{
    public class RequestHelper
    {
        // Methods
        public static string Form(string key)
        {
            string str = HttpContext.Current.Request.Form[key];
            if (str != null)
            {
                return str.Trim();
            }
            return string.Empty;
        }

        public static DateTime GetDate(string str, DateTime defatulValue)
        {
            DateTime time;
            if (!DateTime.TryParse(HttpContext.Current.Request[str], out time))
            {
                time = defatulValue;
            }
            return time;
        }

        public static int GetInt(string str, int defaultValue = 0, HttpContext context = null)
        {
            int num;
            context = context ?? HttpContext.Current;
            if (!int.TryParse(context.Request[str], out num))
            {
                num = defaultValue;
            }
            return num;
        }

        public static string GetIp(HttpContext context = null)
        {
            context = context ?? HttpContext.Current;
            if (context != null)
            {
                string str = context.Request.ServerVariables["REMOTE_ADDR"];
                string str2 = context.Request.ServerVariables["HTTP_VIA"];
                string str3 = string.IsNullOrEmpty(str2) ? str : str2;
                if (!(str3 == "::1"))
                {
                    return str3;
                }
            }
            return "127.0.0.1";
        }

        public static string MapPath(string path)
        {
            if (HttpContext.Current != null)
            {
                return HttpContext.Current.Server.MapPath(path);
            }
            path = Regex.Replace(path, @"\A~?/", "").Replace("/", @"\");
            return Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, path);
        }

        public static string NonHtml(string str)
        {
            return Regex.Replace(str, "</?[^<]+>", "");
        }

        public static string Params(string key)
        {
            string str = HttpContext.Current.Request.Params[key];
            return (str ?? string.Empty);
        }

        public static string Query(string key)
        {
            string str = HttpContext.Current.Request.QueryString[key];
            if (!string.IsNullOrEmpty(str))
            {
                return HttpUtility.UrlDecode(Safe(str.Trim()));
            }
            return string.Empty;
        }

        public static string RawUrl()
        {
            string str = HttpContext.Current.Request.ServerVariables["HTTP_X_REWRITE_URL"];
            if (string.IsNullOrEmpty(str))
            {
                return HttpContext.Current.Request.RawUrl;
            }
            return string.Format("http://{0}{1}", HttpContext.Current.Request.Url.Host, str);
        }

        public static void Rediret301(string url)
        {
            Rediret301(HttpContext.Current, url);
        }

        public static void Rediret301(HttpContext context, string url)
        {
            context.Response.Clear();
            context.Response.Status = "301 Moved Permanently";
            context.Response.AddHeader("Location", url);
            context.Response.End();
        }

        public static string Safe(string str)
        {
            return str.Replace("'", string.Empty);
        }

        public static string UrlParams()
        {
            string str = RawUrl();
            return str.Substring(str.IndexOf('?') + 1);
        }
    }

}
